import {message} from 'ant-design-vue';
export function messageTips(tips,type,time) {    
    message.config({top: '48%',});
    switch(type){
        case 1:    
            message.error({
                duration: time||2,
                maxCount: 1,
                key:type,
                icon:(h)=>{
                    h();
                    return <a-icon type="close-circle" color="#eb5a33" />;
                },
                content:tips,
                onClose:()=>{
                    message.destroy();
                }
            });
        break;
        case 2:
            message.warning({
                duration: time||2,
                maxCount: 1,
                key:type,
                icon:(h)=>{
                    h();
                    return <a-icon type="exclamation-circle" color="#eb5a33" />;
                },
                content:tips,
                onClose:()=>{
                    message.destroy();
                }
            });  
        break;
        case 3:
            message.success({
                duration: time||2,
                maxCount: 1,
                key:type,
                icon:(h)=>{
                    h();
                    return <a-icon type="check-circle" color="#52c41a" />;
                },
                content:tips,
                onClose:()=>{
                    message.destroy();
                }
            });   
        break;
        default:break;
    }
}

export function timeFormat(date,type) {
    var dates = new Date(date).toJSON();
    var result = new Date(+new Date(dates)).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '');
    if(type) {
        result=result.substring(0,16);
    }
    return result;
}

export function checkIsInt(nubmer) { 
    var re = parseInt(nubmer).toString() === 'NaN';
    return !re;
}


/**
 * 手机号码正则验证
 * @param value
 */
export function isPhone(value) {
    var reg = /^1[0-9]{10}$/;
    if (!reg.test(value)) {
        return false;
    } else {
        return true;
    }
}

/**
 * 邮箱正则验证
 * @param value
 */
export function isEmail(value) {
    var reg = /^[0-9a-zA-Z_.-]+[@][0-9a-zA-Z_.-]+([.][a-zA-Z]+){1,2}$/;
    if (!reg.test(value)) {
        return false;
    } else {
        return true;
    }
}

//获取cookie
export function getCookie(sname){
    var acookie = document.cookie.split("; ");
    for (var i = 0; i < acookie.length; i++) {
        var arr = acookie[i].split("=");
        if (sname === arr[0]) {
            if (arr.length > 1)
                return unescape(arr[1]);
            else
                return "";
        }
    }
    return "";
}
//设置cookie
export function setCookie(name,value,option){
    var seconds = option.time*24*60*60 || 0;
    var expires = "";
    if (seconds !== 0 ) {      //设置cookie生存时间
        var date = new Date();
        date.setTime(date.getTime()+(seconds*1000));
        expires = "; expires="+date.toGMTString();
    }
    var path = option.path||'/';
    document.cookie = name+"="+escape(value)+expires+"; path="+path;
}
//删除cookie
export function delCookie(name) {
    setCookie(name,'',{time:-1});
}

export function setSessionItem(key, value) {
    if (typeof (value) !== 'string') {
        value = JSON.stringify(value);
    }
    return window.sessionStorage.setItem(key, value);
}
export function getSessionItem(key) {
    var string = window.sessionStorage.getItem(key);
    try {
        var obj = JSON.parse(string);
        if (typeof obj === 'object' && obj) {
            return obj;
        } else {
            if (string === 'null') {
                return null;
            } else {
                return string;
            }
        }
    } catch (e) {
        if (string === 'null') {
            return null;
        } else {
            return string;
        }
    }
}
export function delSessionItem(name) {
    window.sessionStorage.removeItem(name);
}